
# 授权登录
from common.config import *
import requests,json
from pprint import pprint
from common.Helper import Helper

def authToken( task = None):


    resp = {'type':109,'msg':'更新授权任务','content':{}}

    data = {

        "client_id":secret_key["client_id"],
        "client_secret":secret_key["client_secret"],
        "grant_type":"refresh_token",
        "refresh_token":task['refresh_token']
    }

    r = requests.post(url=domain_url['auth_url'],headers={"Content-Type": "application/json"},data=json.dumps(data))


    # 验证错误
    if 'error_response' in r.json():

        resp['content']['content'] = task
        resp['content']['data'] = {}
        resp['content']['errcode'] = r.json()['error_response']['error_code']
        resp['content']['errmsg'] = r.json()['error_response']['error_msg']

        pprint( resp )

        content = Helper.changeBase64(json.dumps(resp['content']))

        resp['content'] = content.decode('ascii')

        return resp

    else:

        resp['content']['content'] = task
        resp['content']['data'] = r.json()
        resp['content']['errcode'] = 0
        resp['content']['errmsg'] = "任务成功"
        pprint( resp)
        content = Helper.changeBase64(json.dumps(resp['content']))

        resp['content'] = content.decode('ascii')

        return resp



if __name__ == '__main__':

    task = {
          "type": 202,
           "content":  {
            "task_id":  7,
             "owner_id":  186716817,
             "refresh_token":  "f2ba5d25df794e1984fb04c0d304e54d779fbffb"
          },
           "msg":  "更新授权任务"
        }


    task = task['content']

    authToken( task )

